/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applybill.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.dao.gen.bean.BizApplyBill;
import com.nmf.dao.gen.bean.BizApplyBook;
import com.nmf.site.biz.applybill.service.BizApplyBillService;
import com.nmf.site.biz.bill.enums.BillStatusEnums;
import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 申请表Controller
 * @author bm
 * @version 2019-02-16
 */
@Controller
@RequestMapping(value = "${adminPath}/applybill/bizApplyBill")
public class BizApplyBillController extends BaseController {

	@Autowired
	private BizApplyBillService bizApplyBillService;
	
	@ModelAttribute
	public BizApplyBill get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizApplyBill entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizApplyBillService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizApplyBill();
		}
		return entity;
	}
	
	@RequiresPermissions("applybill:bizApplyBill:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizApplyBill bizApplyBill, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyBill> page = bizApplyBillService.findPage(new Page<BizApplyBill>(request, response), bizApplyBill); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/bizApplyBillList";
		}
		return "biz/applybill/bizApplyBillList";
	}
	
	@RequiresPermissions("applybill:bizApplyBill:view")
	@RequestMapping(value = {"provincelist"})
	public String provincelist(BizApplyBill bizApplyBill, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyBill> page = bizApplyBillService.findPageByProvince(new Page<BizApplyBill>(request, response), bizApplyBill); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/province/bizApplyBillList";
		}
		return "biz/applybill/province/bizApplyBillList";
	}
	
	@RequiresPermissions("applybill:bizApplyBill:view")
	@RequestMapping(value = {"countrylist"})
	public String countrylist(BizApplyBill bizApplyBill, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyBill> page = bizApplyBillService.findPage(new Page<BizApplyBill>(request, response), bizApplyBill); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/country/bizApplyBillList";
		}
		return "biz/applybill/country/bizApplyBillList";
	}
	
	
	
	
	@RequiresPermissions("applybill:bizApplyBill:view")
	@RequestMapping(value = "form")
	public String form(BizApplyBill bizApplyBill, Model model) {
		model.addAttribute("bizApplyBill", bizApplyBill);
		return "biz/applybill/bizApplyBillForm";
	}

	@RequiresPermissions("applybill:bizApplyBill:edit")
	@RequestMapping(value = "save")
	public String save(BizApplyBill bizApplyBill, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizApplyBill)){
			return form(bizApplyBill, model);
		}
		String applyBookId = bizApplyBill.getApplyBookId();
		setBookInfo(applyBookId,bizApplyBill);
		try{
			bizApplyBillService.save(bizApplyBill);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applybill/bizApplyBill/?repage";
		}
		addMessage(redirectAttributes, "保存申请表成功");
		return "redirect:"+Global.getAdminPath()+"/applybill/bizApplyBill/?repage";
	}
	
	@RequestMapping(value = {"forForm"})
	public String forForm(@RequestParam(required=true) String bookId, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			BizApplyBill bizApplyBill = new BizApplyBill();
			setBookInfo(bookId,bizApplyBill);

			model.addAttribute("bizApplyBill", bizApplyBill);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/bizApplyBillForm";
		}
		return "biz/applybill/bizApplyBillForm";
	}
	private void setBookInfo(String bookId,BizApplyBill bizApplyBill) {
		BizApplyBook bizApplyBook = bizApplyBillService.forForm(bookId); 
		
		bizApplyBill.setApplyType(bizApplyBook.getApplyType());
		bizApplyBill.setApplyBookId(bizApplyBook.getId());
		bizApplyBill.setBillStatus(BillStatusEnums.INIT.value);
		bizApplyBill.setCategoryId(bizApplyBook.getCategoryId());
		bizApplyBill.setCategoryName(bizApplyBook.getCategoryName());
		bizApplyBill.setProvinceId(bizApplyBook.getProvinceId());
		bizApplyBill.setProvinceName(bizApplyBook.getProvinceName());
	}
	@RequestMapping(value = {"audit"})
	public String audit(@RequestParam(required=true) String id,@RequestParam(required=true) Integer billStatus, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			BizApplyBill bizApplyBill = bizApplyBillService.get(id);
			BillStatusEnums dbBbillStatusEnums = BillStatusEnums.getType(bizApplyBill.getBillStatus());
			BillStatusEnums billStatusEnums = BillStatusEnums.getType(billStatus);
			if(BillStatusEnums.toProvinceAudit.equals(billStatusEnums) && BillStatusEnums.INIT.equals(dbBbillStatusEnums)) {
				
			}else if(BillStatusEnums.toCountryAudit.equals(billStatusEnums) && BillStatusEnums.toProvinceAudit.equals(dbBbillStatusEnums)) {
				bizApplyBill.setProvinceAuditId(UserUtils.getUser().getId());
				bizApplyBill.setProvinceAuditName(UserUtils.getUser().getName());
			}else if(BillStatusEnums.auditSuccess.equals(billStatusEnums) && BillStatusEnums.toCountryAudit.equals(dbBbillStatusEnums)) {
				bizApplyBill.setCountryAuditId(UserUtils.getUser().getId());
				bizApplyBill.setCountryAuditName(UserUtils.getUser().getName());
			}else if(BillStatusEnums.failed.equals(billStatusEnums) ) {
			}else {
				throw new SiteException("状态错误");
			}
			bizApplyBill.setBillStatus(billStatus);
			bizApplyBillService.save(bizApplyBill);
			
			model.addAttribute("bizApplyBill", bizApplyBill);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybill/bizApplyBillForm";
		}
		return "biz/applybill/bizApplyBillForm";
	}
	
	@RequiresPermissions("applybill:bizApplyBill:edit")
	@RequestMapping(value = "delete")
	public String delete(BizApplyBill bizApplyBill, RedirectAttributes redirectAttributes) {
		try{
			bizApplyBillService.delete(bizApplyBill);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applybill/bizApplyBill/?repage";
		}
		addMessage(redirectAttributes, "删除申请表成功");
		return "redirect:"+Global.getAdminPath()+"/applybill/bizApplyBill/?repage";
	}

}